package com.xt.core.helper.page;

/**
 * a service to get currentPage object
 * 
 * @author txiao
 *
 */
public class PagerService {
	
	/**
	 * get the current page object
	 *  
	 * @param currentPage  current pagenumber
	 * @param pagerMethod  the page method next, previous, first, last
	 * @param totalRows    total rows
	 * @return  the current page object
	 */
    public static Pager getPager(String currentPage,String pagerMethod,int totalRows) {
        //the page object
        Pager pager = new Pager(totalRows);
        //if the currentPage is null, means the first time to access the page
        //if not null, refresh the page with the current page number
        if (currentPage != null) {
            pager.refresh(Integer.parseInt(currentPage));
        }
        //get the pager method
        if (pagerMethod != null) {
            if (pagerMethod.equals("first")) {
                pager.first();
            } else if (pagerMethod.equals("previous")) {
                pager.previous();
            } else if (pagerMethod.equals("next")) {
                pager.next();
            } else if (pagerMethod.equals("last")) {
                pager.last();
            }
        }
        return pager;
    }
    
    /**
	 * get the current page object
	 *  
	 * @param currentPage  current pagenumber
	 * @param pagerMethod  the page method next, previous, first, last
	 * @param totalRows    total rows
	 * @return  the current page object
	 */
    public static Pager getPager(String currentPage,String pagerMethod,int totalRows, int pageSize) {
        //the page object
        Pager pager = new Pager(totalRows, pageSize);
        //if the currentPage is null, means the first time to access the page
        //if not null, refresh the page with the current page number
        if (currentPage != null) {
            pager.refresh(Integer.parseInt(currentPage));
        }
        //get the pager method
        if (pagerMethod != null) {
            if (pagerMethod.equals("first")) {
                pager.first();
            } else if (pagerMethod.equals("previous")) {
                pager.previous();
            } else if (pagerMethod.equals("next")) {
                pager.next();
            } else if (pagerMethod.equals("last")) {
                pager.last();
            }
        }
        return pager;
    }
    
    /**
     * open the next page directly
     * 
     * @param nextPage the next page number
     * @param totalRows total rows
     * @return the next page object
     */
    public static Pager getPager(String nextPage,int totalRows) {
        //the page object
        Pager pager = new Pager(totalRows);
        //not the first page
        if(nextPage !=null)
        	pager.open(Integer.parseInt(nextPage));
        return pager;
    }

    
}
